<?php
    include_once("db_connect.php");

if($_SESSION['logged']!='stat') {

/*
 pro_name, pro_date_begin, pro_date_end, pro_prefix, pro_caller_id, pro_max_retries, pro_total_max_retries, pro_wait_time, pro_retry_time, pro_context, pro_lines,
 '".$_POST["name"]."', '".$_POST["date_begin"]."', '".$_POST["date_end"]."', '".$_POST["prefix"]."', '".$_POST["caller_id"]."', ".$_POST["max_retries"].", ".$_POST["total_max_retries"].", ".$_POST["wait_time"].", ".$_POST["retry_time"].", '".$_POST["context"]."', ".$_POST["lines"]."
*/

    if($_POST["edid"]==-1){
	pg_exec("INSERT INTO public.caller_project 
		    (pro_id, pro_name, pro_date_begin, pro_date_end, 
			pro_prefix, pro_caller_id, pro_max_retries, pro_total_max_retries, 
			pro_wait_time, pro_retry_time, pro_context) 
		    VALUES 
		    (nextval('caller_project_pro_id_seq'), '".$_POST["name"]."', '".$_POST["date_begin"]."', '".$_POST["date_end"]."', 
			'".$_POST["prefix"]."', '".$_POST["caller_id"]."', ".$_POST["max_retries"].", ".$_POST["total_max_retries"].", 
			".$_POST["wait_time"].", ".$_POST["retry_time"].", '".$_POST["context"]."')");
	header("Location: /caller/projects.php");

    } elseif (is_numeric($_POST["edid"])){
	pg_exec("UPDATE public.caller_project SET 
	     pro_name = '".$_POST["name"]."', 
	     pro_date_begin = '".$_POST["date_begin"]."', 
	     pro_date_end = '".$_POST["date_end"]."', 
	     pro_prefix = '".$_POST["prefix"]."', 
	     pro_caller_id = '".$_POST["caller_id"]."', 
	     pro_max_retries = ".$_POST["max_retries"].", 
	     pro_total_max_retries = ".$_POST["total_max_retries"].", 
	     pro_wait_time = ".$_POST["wait_time"].", 
	     pro_retry_time = ".$_POST["retry_time"].", 
	     pro_context = '".$_POST["context"]."', 
	     pro_lines = 0
	    WHERE pro_id='".$_POST["edid"]."'");
	header("Location: /caller/projects.php");
    }
    if(is_numeric($_GET["delid"])){
	pg_exec("DELETE FROM public.caller_project WHERE pro_id = '".$_GET["delid"]."'");
	header("Location: /caller/projects.php");
    }
    if(is_numeric($_GET["editid"])){
	$to_edit = pg_exec("SELECT * FROM public.caller_project WHERE pro_id = '".$_GET["editid"]."'");
	$edit = pg_fetch_array($to_edit);
    }

}
    
?>
<?php
    include_once("menu.php");
?>
<h2>Projects</h2>
<form method="post">

<?
    if($_SESSION['logged']!='stat') :
?>
Name (Odessa Fiz.2) : <input type="text" size="12" maxlength="20" value="<?=$edit['pro_name']?>" name="name"><br />
Begin date (2009-04-29) : <input type="text" size="10" maxlength="10" value="<?=$edit['pro_date_begin']?>" name="date_begin"><br />
End date (2009-08-29) : <input type="text" size="10" maxlength="10" value="<?=$edit['pro_date_end']?>" name="date_end"><br />
Prefix (IAX2/pbx-odessa-cc/) : <input type="text" size="12" maxlength="20" value="<?=$edit['pro_prefix']?>" name="prefix"><br />
Caller ID (487207149) : <input type="text" size="12" maxlength="20" value="<?=$edit['pro_caller_id']?>" name="caller_id"><br />
Max retries (0) : <input type="text" size="4" maxlength="4" value="<?=$edit['pro_max_retries']?>" name="max_retries"><br />
Total Max Retries (5) : <input type="text" size="4" maxlength="4" value="<?=$edit['pro_total_max_retries']?>" name="total_max_retries"><br />
Wait Time (15) : <input type="text" size="4" maxlength="4" value="<?=$edit['pro_wait_time']?>" name="wait_time"><br />
Retry Time (15) : <input type="text" size="4" maxlength="4" value="<?=$edit['pro_retry_time']?>" name="retry_time"><br />
Context (odessa-voice-message) : <input type="text" size="30" maxlength="30" value="<?=$edit['pro_context']?>" name="context"><br />

    <input type="hidden" value="<?=(isset($edit['pro_id']))?$edit['pro_id']:-1?>" name="edid">
    <input type="submit" value="submit" name="submit"><br />

</form>

<?endif;?>

<?php
    $new = pg_exec('select * from public.caller_project');
?>

<table border=1>
<tr>
<td></td>
<td></td>
 <td>id</td>
 <td>name</td>
 <td>date_begin</td>
 <td>date_end</td>
 <td>prefix</td>
 <td>caller_id</td>
 <td>max_retries</td>
 <td>total_max_retries</td>
 <td>wait_time</td>
 <td>retry_time</td>
 <td>context</td>
</tr>

<?php    
    while($t = pg_fetch_array($new)){
        echo '<tr>';
	echo '<td><a href=?delid='.$t[0].'>delete</a></td>';
	echo '<td><a href=?editid='.$t[0].'>edit</a></td>';

	echo '<td>'.$t['pro_id'].'</td>';

echo '<td>'.$t['pro_name'].'</td>';
echo '<td>'.$t['pro_date_begin'].'</td>';
echo '<td>'.$t['pro_date_end'].'</td>';
echo '<td>'.$t['pro_prefix'].'</td>';
echo '<td>'.$t['pro_caller_id'].'</td>';
echo '<td>'.$t['pro_max_retries'].'</td>';
echo '<td>'.$t['pro_total_max_retries'].'</td>';
echo '<td>'.$t['pro_wait_time'].'</td>';
echo '<td>'.$t['pro_retry_time'].'</td>';
echo '<td>'.$t['pro_context'].'</td>';

        echo '</tr>';
    }
?>

</table>

<?php    
    include_once("db_disconnect.php");
?>